/* This do-file replicates Figure 5 of the paper */ 

cd "" /*input directory here with cleaned data appended_census_mothers using census preparation do-file*/

set more off 

use appended_census_mothers.dta, clear 

********************************************************************************
* Adjust Cohorts to match GHS, see text for detailed explanation * 

gen child_cohort_ghs=child_cohort
replace child_cohort_ghs=child_cohort+1 if yc_birth_month>=7

drop child_cohort 
rename child_cohort_ghs child_cohort 

********************************************************************************
********************************************************************************
*****************
* Sample Select *  
*****************
drop if race==3 | race==4 /* Exclude whites and Indian/Asian, who do not receive the CSG, see text */

* Select Windows * 

keep if child_cohort>=1987 & child_cohort<=1998

** Exclude missing values ** 

drop if emp==1 & (occupation==. | industry==.) &  (year==2001 | year==2007 | year==2011)
drop if emp==. | activity==.

replace yc_birth_month=99 if yc_birth_month==. /*Indicator variable if month of birth missing */

*********************************************************************************

** Controls ** 

local controls i.race  age c.age#c.age yrs_schooling household_size i.province i.yc_birth_month


** Obtain residuals by regressing on controls, by year ** 

foreach k of numlist 2001 2007 2011{

reg activity `controls' if year==`k' [w=weight]
predict activity`k' if year==`k', res 

reg unemployed `controls' if year==`k' [w=weight] 
predict unemployed`k' if year==`k', res
}


drop activity
drop unemployed

gen activity=activity2001 if year==2001 
replace activity=activity2007 if year==2007 
replace activity=activity2011 if year==2011 


gen unemployed=unemployed2001 if year==2001 
replace unemployed=unemployed2007 if year==2007 
replace unemployed=unemployed2011 if year==2011 

**********************************************************


gen act_share=.
gen unemp_share=. 

egen child_cohort_d=group(child_cohort)

foreach k of numlist 2001 2007 2011{
forvalues j= 0 (1) 1{ 
forvalues i= 1 (1) 12 { 


summarize activity [w=weight] if child_cohort_d==`i' & married==`j' & year==`k'
replace act_share=r(mean) if child_cohort_d==`i' & married==`j' & year==`k'

summarize unemployed [w=weight] if child_cohort_d==`i' & married==`j' & year==`k'
replace unemp_share=r(mean) if child_cohort_d==`i' & married==`j' & year==`k'


}
}
}


*********
* Fig 5 *
*********
cd "C:\Users\alessandro\Il mio Drive\CSG impact on Job quality\WBER\Replication package\Figures"

egen tag=tag(child_cohort year married)

forvalues j= 0 (1) 1{ 

foreach k of numlist 2001 2007 2011 {
preserve 

keep if year==`k' & married==`j'


twoway (lfitci unemployed child_cohort [aw=weight] if child_cohort<1993, range(. 1993)  ciplot(rline) lcolor(black) clwidth(medthick)  alcolor(gs10) alstyle(none) alpattern(dash)  fcolor(none))  ///
|| (lfitci unemployed child_cohort [aw=weight] if child_cohort>=1993 ,  ciplot(rline) lcolor(black) clwidth(medthick)  alcolor(gs10) alstyle(none) alpattern(dash)  fcolor(none)) ///
|| (scatter unemp_share child_cohort if tag==1, xlabel(1987 (2) 1998) xline(1993, lcolor(black)) graphregion( color(white) ) plotregion(fcolor(white) ) msize(small) mcolor(black)  ) /// 
||, legend(off) xtitle("") ylabel(-0.08 (0.02) 0.08)

graph export Tondini_fig5_`k'_`j'.png, replace

restore
}
}
